package net.zjitc.dao.impl;

import net.zjitc.dao.CategorySecondDao;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by 沈银 on 2017/11/18.
 */
public class CategorySecondDaoImpl implements CategorySecondDao{
    public List<CategorySecond> findAll() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from categorysecond";
        List<CategorySecond> list=null;
        try {
            list= (List<CategorySecond>) runner.query(sql,new BeanListHandler(CategorySecond.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int addCategorySecond(CategorySecond categorySecond) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into categorysecond(csname,cid) values(?,?)";
        try {
            int count=runner.update(sql,categorySecond.getCsname(),categorySecond.getCategory().getCid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public CategorySecond findByCsid(String csid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from categorysecond where csid=?";
        try {
            //Map将字段名和对应的值封装成一个map集合
            /*适用于一个字段对应一个值，且有两个类时
            * csid  1
            * cname ***
            * cid   1
            * */
            Map<String,Object> map=runner.query(sql,new MapHandler(),csid);
            //创建两个实例
            Category category=new Category();
            CategorySecond categorySecond=new CategorySecond();
            //将相应的内容放进去
            BeanUtils.populate(category,map);
            BeanUtils.populate(categorySecond,map);
            categorySecond.setCategory(category);
            return categorySecond;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int editCategorySecond(CategorySecond categorySecond) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update categorysecond set csname=?,cid=? where csid=?";
        try {
            int count=runner.update(sql,categorySecond.getCsname(),categorySecond.getCategory().getCid(),categorySecond.getCsid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteCategorySecond(String csid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="delete from categorysecond where csid=?";
        try {
            int count=runner.update(sql,csid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<CategorySecond> findByCid(int cid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from categorysecond where cid=?";
        try {
            List<CategorySecond> list= (List<CategorySecond>) runner.query(sql,new BeanListHandler(CategorySecond.class),cid);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
